<template>
   <div id="shoppingCar">
      <div class="shoppingCarContainer">
            <ul>
                <li class="shoppingCarProducts" v-for="shoppingCarItem in shoppingCarData">
                  <div class="shoppingCarProduct">
                    
                   <div class="shoppingCarProductImg">
                   <el-checkbox name="type" v-model="shoppingCarItem.checked" @change="selectProduct(shoppingCarItem.checked)"></el-checkbox>
                       <img :src="shoppingCarItem.miao">   
                   </div>
                   <div class="shoppingCarProductContent">
                   <h4 class="media-heading">{{shoppingCarItem.name}}</h4>
                    <p class="xs-none">SKU： W65-107921</p>
                   <p class="">尺寸：1257 x 813 x 800 mm</p>
                   <p class="xs-none">材质：栗子木，实木，织物</p>
                   <p class="xs-none">品牌：WILLO</p>  
                   </div>

                   
                  <div class="shoppingCarProductNum">
                     <el-input v-model="shoppingCarItem.number">
                      <el-button slot="prepend" class="el-icon-minus" @click="changeNum(shoppingCarItem,0)"></el-button>
                      <el-button slot="append" class="el-icon-plus" @click="changeNum(shoppingCarItem,1)"></el-button>
                    </el-input>  
                  </div>
                  

                     <strong class="shoppingCarMoney">${{shoppingCarItem.price }}</strong>

                     <i class="el-icon-delete2" @click="delConfirm(shoppingCarItem)"></i>
                     <i class="el-icon-star-off"></i>
                  </div>
                   
                </li>
            </ul>


            <div id="fixedCtrl" class="">
            <div class="shoppingCarProductResult">
              <div class="shoppingCarProductResultLeft">
                 <el-checkbox label="全选" name="type" @change="selectAll(true)"></el-checkbox>

               <el-button class="xs-none" type="text">删除</el-button>
               <el-button class="xs-none" type="text">清除失效宝贝</el-button>
               <el-button class="xs-none" type="text">移入收藏夹</el-button>
               <el-button class="xs-none" type="text">分享</el-button>
              </div>
                    
              <div class="shoppingCarProductResultRight">
              <span class="xs-none">
               已选择{{allProduct}}件商品 
              </span>
               <span>
                  总价：<span>{{allMoney | formatMoney}}</span>
               </span>

               <router-link :to="{ path: 'selectAddress'}">
              <el-button type="danger">去结算</el-button> 
              </router-link>
              </div>

               
              </div>

            </div>
                 </div>

                 <el-dialog title="删除商品" v-model="dialogVisible" size="tiny">
  <span>确定删除吗？</span>
  <span slot="footer" class="dialog-footer">
    <el-button @click="dialogVisible = false">取 消</el-button>
    <el-button type="primary" @click="delResult(shoppingCarItem)">确 定</el-button>
  </span>
</el-dialog>
   </div>
</template>


<script>
export default{
    name: 'shoppingCar',
  data () {
    return {
      scrolled: 0,
      contentHeight: 0,
      height: 0,
      top: 0,
      shoppingCarData: [],
      selectAllProduct:false,
      allMoney: 0,
      dialogVisible: false,
      shoppingCarItemProduct:'' ,
      allProduct: 0
    }
  },
  computed: {
  
  },
  mounted (){
      this.getShoppingCarData();
    window.addEventListener('scroll', this.handleScroll);
    this.contentHeight =  parseInt(this.length);
    this.height = document.documentElement.clientHeight;
    
    this.fixedCtrl();
  },
  filters: {
    formatMoney: function(value){
      return "$" + value.toFixed(2);
    }
  },
  methods: {
   handleScroll () {
    this.scrolled = window.scrollY ;
    this.top = this.scrolled + this.height - 180;
    
    //console.log(this.shoppingCarData.length*142)
    this.fixedCtrl();
  },
    getShoppingCarData: function(){
    this.$http.get('static/data/productList.json').then((response) => {
                   this.shoppingCarData= response.body;
                })
                .catch(function(response) {
                    console.log(response)
                })
    },
    fixedCtrl: function(){
    if(window.localStorage.getItem("windowWidth")>1024){
       if(this.top>this.shoppingCarData.length*142){
        document.getElementById("fixedCtrl").classList.remove("shoppingCarProductResultFixed");
    }else{
      document.getElementById("fixedCtrl").classList.add("shoppingCarProductResultFixed");
    }
    }else{
     document.getElementById("fixedCtrl").classList.add("shoppingCarProductResultFixed"); 
    }
    },
    changeNum: function(shoppingCarItem,way){
      if(way==1){
        shoppingCarItem.number++;
      }else{
        if(shoppingCarItem.number<2){
          shoppingCarItem.number =1;
        }else{
           shoppingCarItem.number--;   
        }
       
      };
       this.countAllMoney();
    },
    selectProduct: function(item){
         console.log(item);
         if(item){
           this.allProduct ++;  
         }else{
          this.allProduct --;
         }
      this.countAllMoney();
    },
    selectAll: function(falg){
      this.selectAllProduct = !this.selectAllProduct;
      this.allProduct =0;
      this.shoppingCarData.forEach((item,index)=>{
        this.$set(item,"checked",this.selectAllProduct);
        if(this.selectAllProduct){
          this.allProduct ++;  
        }
        
      });
       this.countAllMoney();
     /* var _this = this;
      this.shoppingCarData.forEach(function(item,index){
        _this.$set(item,"checked",_this.selectAllProduct);
      })*/

    },
    countAllMoney: function(){
      this.allMoney = 0;
      this.shoppingCarData.forEach((item,index)=>{
        if(item.checked){
          this.allMoney += parseInt(item.price)*parseInt(item.number);
           console.log(this.allMoney)
        }
      });
     /* var _this = this;
      this.shoppingCarData.forEach(function(item,index){
        _this.allMoney = item.price*item.number;
        console.log(_this.allMoney)
      })*/
    },
    delConfirm: function(shoppingCarItem){
        this.shoppingCarItemProduct = shoppingCarItem;
        this.dialogVisible = true;
    },
    delResult: function(shoppingCarItem){
        let index = this.shoppingCarData.indexOf(this.shoppingCarItemProduct);
        this.shoppingCarData.splice(index,1);
         this.dialogVisible = false;
    }
  },
  watch:{
       
  }
}
</script>

<style type="text/css">
.shoppingCarProducts{
  width: 100%;
}
  .shoppingCarProduct,.shoppingCarProductResult{
    border: 1px solid #ddd;
    padding: 10px 0;
    overflow: hidden;
  }
  .shoppingCarProductResultFixed{
        position: fixed;
    bottom: 0;
    width: 100%;
    left: 0;
    border-top: 1px solid #ddd;
    background: white;
    z-index: 1; 
  }
  .shoppingCarProductResultFixed .shoppingCarProductResult{
    width: 1160px;
    margin: 0 auto;
    padding-left: 50px;
    border: none;
  }

  .shoppingCarProductImg img,.shoppingCarProductImg{
    width: 120px;
    height: 120px;
  }
  .shoppingCarProductImg{
    position:relative;
    margin-left: 50px;
        float: left;
  }
  .shoppingCarProductContent{
   float: left; 
   text-align: left;
   margin-left: 30px
  }
  .shoppingCarProductContent p{
    font-size: .9rem;
    line-height: 1.5rem;
  }

  .shoppingCarProduct .el-checkbox{
    position: absolute;
        left: -30px;
    top: 50%;
    margin-top: -9px;
  }
  .shoppingCarProductNum{
    width: 200px;
     float: left;
     margin-left: 200px;
     margin-top: 35px;
  }
  .shoppingCarMoney{
    color: #ff4400;
    font-size: 16px;
    line-height: 34px;
    font-weight: bold;
    margin-left: 30px;
        display: inline-block;
        margin-top: 35px;

  }
  .el-icon-delete2,.el-icon-star-off{
    font-size: 24px;
    line-height: 34px;
    cursor: pointer;
    margin-left: 30px;

  }
  .shoppingCarProductResultLeft{
    float: left;
    margin-left: 20px;
  }
  .shoppingCarProductResultRight{
    float: right;
    margin-right: 20px;
  }
  .shoppingCarProductResultRight>span{
    padding-right: 25px;
  }

  @media (min-width: 1200px){

    .shoppingCarContainer{
      width: 1160px;
      margin:0 auto; 
      padding-left: 50px;
    }
  }

  @media (min-width: 992px) and (max-width: 1199px){
.shoppingCarContainer{
      margin: 0 20px;
    }
    .shoppingCarProductNum{
     margin-left: 150px;
  }
   .shoppingCarProductResultFixed .shoppingCarProductResult {
     width: auto;
    padding-left: 20px;
}
  }

    @media (min-width: 768px) and (max-width: 991px){
.shoppingCarContainer{
      margin: 0 20px;
    }
    .shoppingCarProductNum{
     margin-left: 30px;
     width: 130px
  }
  .shoppingCarContainer .el-input-group__append,.shoppingCarContainer .el-input-group__prepend {
    padding: 0 5px;
}
.el-icon-delete2, .el-icon-star-off {
    margin-left: 5px;
}
.shoppingCarMoney{
    margin-left: 20px;

  }
  .shoppingCarProductResultFixed .shoppingCarProductResult {
     width: auto;
    padding-left: 20px;
}
  }

@media (max-width: 767px){
  .shoppingCarProduct {
    border:none;
    border-bottom: 1px solid #ddd; 
}
  .shoppingCarContainer{
      margin: 0 8px;
    }
    .shoppingCarProductImg img, .shoppingCarProductImg {
    width: 90px;
    height: 90px;
}
    .shoppingCarProductImg {
    margin-left: 25px;
  }
  .shoppingCarProduct .el-checkbox {
    left: -25px;
}
.xs-none{
  display: none;
}
  .shoppingCarProductContent {
    margin-left: 10px;
}
  .shoppingCarProductContent p {
    font-size: .85rem;
    line-height: 2.3rem;
}
.shoppingCarMoney {
    margin-left: 0px;
    margin-top: 0px;
}
.el-icon-delete2, .el-icon-star-off {
        font-size: 20px;
    margin-left: 0;
    float: right;
    margin-right: 5px;

}
 .shoppingCarProductNum{
     margin-left: 10px;
     width: 80px;
     margin-top: 0
  }
  .shoppingCarContainer .el-input-group__append,.shoppingCarContainer .el-input-group__prepend {
    padding: 0 5px;
    font-size: 12px;
}
 .shoppingCarContainer .el-input__inner{
  height: 30px;
}

.shoppingCarContainer .el-button{
      padding: 8px 10px;
}
 .shoppingCarProductResultFixed .shoppingCarProductResult {
     width: auto;
    padding-left: 0px;
}
.shoppingCarProductResultLeft {
    float: left;
    margin-left: 10px;
    margin-top: 5px;
}

}

</style>